var wxCharts = require('../../../../wxcharts/wxcharts.js')
Page({
  data: {
    advImage: [{
      url: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1572861372260&di=1d15b314fc77d1f504601f9813b6b3cc&imgtype=jpg&src=http%3A%2F%2Fimg0.imgtn.bdimg.com%2Fit%2Fu%3D3174449900%2C50903893%26fm%3D214%26gp%3D0.jpg'
    },
    ],

    sameBooks:[],

    istokenPop: false, //判断是否等了


    isnumPop: false, //判断是否手机号授权

    isAdd: false, //是否添加购物车

    isCollect: false,

    data: null,
    isYuding: null,
    count: 0,
    lookMore: false,

    donateUserRecords: [], //卖过人数
    availableNum: null,
    maximumDiscount: 0,
    surplusPrice: 0,

    TrolleyBookNum: 0,

    introduction: '',

    introduction2: '',


    list: [],
    params: {
      page: 1,
      size: 5,
      bookInfoId: ''
    },


    array: ['品相优良', '略有瑕疵', '新书'],
    index: 0,

    selectorChecked: "品相优良",

    openPop: false,

    openPop2: false,


    availableNum: '',
    maximumDiscount: 0,
    maximumDiscounts: 0,
    surplusPrice: 0,


    bookPrice: 0,


    fineBookPrice: 0,


    defectiveBookPrice: 0,

    newBookPrice: 0,


    fineAvailableNum: 0,


    bookQuality: 1,


    newBookAvailableNum: null,
    defAvailableNum: null,


    choosefangshi: 1, //选择方式

    chooseOpen: null
  },


  // 跳转扫码卖书
  //跳转卖书
  saoma() {
    wx.switchTab({
      url: '/pages/juanshu/juanshu',
    })
  },
  //接口部分

  gotoBookdetail(e) {
    var bookId = e.currentTarget.dataset.bookid
    wx.navigateTo({
      url: '/package/index/pages/bookdetail/bookdetail?bookId=' + bookId,
    })
  },

  getBookDetail(id) {

    var _this = this
    var params = {
      id: id
    }

    var url = wx.getStorageSync('token') != '' ? 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/book/bookinfo/detail/auth' : 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/book/bookinfo/detail2'
    wx.request({
      url: url,
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        console.log(res)

        var availableNum = res.data.data.availableNum

        var newBookAvailableNum = res.data.data.newBookAvailableNum

        var defAvailableNum = res.data.data.defAvailableNum

        if (res.data.code == 200) {

          _this.setData({

            data: res.data.data,
            coverPic: res.data.data.coverPic,
            bookPrice: res.data.data.bookPrice,
            name: res.data.data.name,
            price: res.data.data.price,
            author: res.data.data.author,
            publisher: res.data.data.publisher,
            bindingStyle: res.data.data.bindingStyle,
            scoreDouban: res.data.data.scoreDouban,
            introduction: res.data.data.introduction,
            introduction2: res.data.data.introduction.substring(0, 50),
            donateUserRecords:
              res.data.data.donateUserRecords.length > 3
                ? res.data.data.donateUserRecords.splice(0, 3)
                : res.data.data.donateUserRecords,
            availableNum: res.data.data.availableNum,  //品相优良
            maximumDiscount: res.data.data.maximumDiscount,
            maximumDiscounts: res.data.data.maximumDiscounts,
            surplusPrice: res.data.data.surplusPrice,
            // sdxScores: res.data.data.sdxScores,
            fineBookPrice: res.data.data.fineBookPrice,
            defectiveBookPrice: res.data.data.defectiveBookPrice,
            newBookPrice: res.data.data.newBookPrice,


            fineAvailableNum: res.data.data.fineAvailableNum,
            newBookAvailableNum: res.data.data.newBookAvailableNum, //全新书籍库存


            defAvailableNum: res.data.data.defAvailableNum,//略有瑕疵书籍库存

            array: availableNum != 0 && newBookAvailableNum != 0 && defAvailableNum != 0 ?
              ['品相优秀', '略有瑕疵', '新书'] : availableNum != 0 && newBookAvailableNum == 0 && defAvailableNum != 0 ?
                ['品相优秀', '略有瑕疵'] : availableNum != 0 && newBookAvailableNum != 0 && defAvailableNum == 0 ?
                  ['品相优秀', '新书'] : availableNum != 0 && newBookAvailableNum == 0 && defAvailableNum == 0 ?
                    ['品相优秀'] : availableNum == 0 && newBookAvailableNum == 0 && defAvailableNum == 0 ?
                      ['品相优秀'] : availableNum == 0 && newBookAvailableNum != 0 && defAvailableNum == 0 ?
                        ['新书'] : availableNum == 0 && newBookAvailableNum != 0 && defAvailableNum != 0 ?
                          ['略有瑕疵', '新书'] : availableNum == 0 && newBookAvailableNum == 0 && defAvailableNum != 0 ?
                            ['略有瑕疵'] : ['品相优秀'],

            selectorChecked: availableNum != 0 && newBookAvailableNum != 0 && defAvailableNum != 0 ?
              '品相优秀' : availableNum != 0 && newBookAvailableNum == 0 && defAvailableNum != 0 ?
                '品相优秀' : availableNum != 0 && newBookAvailableNum != 0 && defAvailableNum == 0 ?
                  '品相优秀' : availableNum != 0 && newBookAvailableNum == 0 && defAvailableNum == 0 ?
                    '品相优秀' : availableNum == 0 && newBookAvailableNum == 0 && defAvailableNum == 0 ?
                      '品相优秀' : availableNum == 0 && newBookAvailableNum != 0 && defAvailableNum == 0 ?
                        '新书' : availableNum == 0 && newBookAvailableNum != 0 && defAvailableNum != 0 ?
                          '略有瑕疵' : availableNum == 0 && newBookAvailableNum == 0 && defAvailableNum != 0 ?
                            '略有瑕疵' : '品相优秀',

            bookQuality: availableNum != 0 && newBookAvailableNum != 0 && defAvailableNum != 0 ?
              1 : availableNum != 0 && newBookAvailableNum == 0 && defAvailableNum != 0 ?
                1 : availableNum != 0 && newBookAvailableNum != 0 && defAvailableNum == 0 ?
                  1 : availableNum != 0 && newBookAvailableNum == 0 && defAvailableNum == 0 ?
                    1 : availableNum == 0 && newBookAvailableNum == 0 && defAvailableNum == 0 ?
                      1 : availableNum == 0 && newBookAvailableNum != 0 && defAvailableNum == 0 ?
                        0 : availableNum == 0 && newBookAvailableNum != 0 && defAvailableNum != 0 ?
                          2 : availableNum == 0 && newBookAvailableNum == 0 && defAvailableNum != 0 ?
                            2 : 1
          })

          _this.getDisconprice(_this.data.bookId, _this.data.bookQuality, 2)

        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },


  getFangshi (e) {
    console.log(e)

    this.setData({
      choosefangshi: e.currentTarget.dataset.fangshi
    })
  },

  getFangshi2 (e) {
      this.setData({
        choosefangshi: e.currentTarget.dataset.fangshi
      })
      this.addCarApi(this.data.bookId,0)
  },

  getFangshi5 () {
    wx.showToast({
      title: '书币不足',
      icon: 'none'
    })
    return
  },

  getFangshi6 (e) {
    this.setData({
      choosefangshi: e.currentTarget.dataset.fangshi
    })
    this.addCarApi(this.data.bookId, this.data.maximumDiscount)
  },

  addCarApi(id, maximumdiscount) {
    var _this = this

    wx.showLoading({
      title: "购物车添加中",
      icon: "none"
    })
    var params = {
      userId: wx.getStorageSync('userId'),
      bookInfoId: id,
      bookQuality: maximumdiscount != 0 && _this.data.bookQuality == 1 ? -1 : maximumdiscount != 0 && _this.data.bookQuality == 2 ? -3 : maximumdiscount != 0 && _this.data.bookQuality == 0 ? -2 : _this.data.bookQuality,
      shopScores: maximumdiscount
    }

    wx.request({
      url:
        'https://devapi.xiaoshitimebank.com/user/sdx/sdx/sdxShoppingTrolleys/mod/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          wx.hideLoading()


          // if (_this.data.fromType == 1) {
            if (JSON.stringify(res.data.data)!='{}') {
              // wx.showToast({
              //   title: '加入购物车成功',
              //   icon: 'none'
              // })
            var params2 = {
              "id": res.data.data.shoopingTrolleyId,
              "userId": wx.getStorageSync('userId'),
              "bookInfoId": _this.data.bookId,
              "serviceId": "",
              "ischoose": false,
              "bookInfoName": _this.data.data.name,
              "bookInfoCoverPic": _this.data.data.coverPic,
              "price": res.data.data.Price,
              "suPrice": res.data.data.suprice,
              "updateTime": "",
              "shopScores": maximumdiscount,
              "bookQuality": params.bookQuality
            }


              wx.showToast({
                title: '加入购物车成功',
                icon: 'none',
                duration: 2000
              })

              _this.getCardnum(_this.data.bookId)




              // var num = _this.data.TrolleyBookNum

              // num += 1


              // _this.setData({
              //   TrolleyBookNum: num,
              //   wasTrolleyBook: true
              // })

            if (_this.data.fromType == 1) {
              var pages = getCurrentPages();//获取页面栈
              var beforePage = pages[pages.length - 2];

              var beforePage2 = pages[pages.length - 3]

              beforePage.changeData2(params2)

              beforePage2.changeData8()

              setTimeout(function () {
                wx.navigateBack()
              }, 2000)
            }


          }else{
            wx.showToast({
              title: '请不要重复添加',
              icon: 'none'
            })
            }
          //   return
          // }




          // wx.navigateBack()


          // var num = _this.data.TrolleyBookNum

          // num+=1

          // _this.setData({
          //   TrolleyBookNum: num,
          //   wasTrolleyBook: true
          // })


          // _this.getDisconprice(_this.data.bookId,_this.data.bookQuality, 1)
          _this.setData({
            isAdd: true
          })

          setTimeout(function () {
            _this.setData({
              openPop2: false
            })
          }, 2000)
          setTimeout(function () {
            _this.setData({
              isAdd: false
            })
          }, 2000)
        } else {
          _this.setData({
            isAdd: false
          })
        }
      }
    })
  },

  getNewjisuan(e) {
    var _this = this
    this.setData({
      bookQuality: e.currentTarget.dataset.quid,
      selectorChecked: e.currentTarget.dataset.dex
    })


      _this.getDisconprice(_this.data.bookId, _this.data.bookQuality, 2)



  },

  closeBuy () {
    this.setData({
      openPop: false
    })
  },

  closeBuy2 () {
    this.setData({
      openPop2: false
    })
  },

  bindPickerChange(e) {
    var _this = this

    console.log(e)
    var num1 = this.data.availableNum
    var num2 = this.data.newBookAvailableNum
    var num3 = this.data.defAvailableNum
    this.setData({
      selectorChecked: this.data.array[e.detail.value]
    })


    if (num1 != 0 && num2 != 0 && num3 != 0) {

      if (e.detail.value == 0) {
        this.setData({
          bookQuality: 1
        })
      }


      if (e.detail.value == 1) {
        this.setData({
          bookQuality: 2
        })
      }

      if (e.detail.value == 2) {
        this.setData({
          bookQuality: 0
        })
      }
    }

    if (num1 != 0 && num2 == 0 && num3 != 0) {
      if (e.detail.value == 0) {
        this.setData({
          bookQuality: 1
        })
      }


      if (e.detail.value == 1) {
        this.setData({
          bookQuality: 2
        })
      }
    }

    if (num1 != 0 && num2 != 0 && num3 == 0) {
      if (e.detail.value == 0) {
        this.setData({
          bookQuality: 1
        })
      }


      if (e.detail.value == 1) {
        // debugger
        this.setData({
          bookQuality: 0
        })
      }
    }

    if (num1 != 0 && num2 == 0 && num3 == 0) {
      if (e.detail.value == 0) {
        this.setData({
          bookQuality: 1
        })
      }
    }

    if (num1 == 0 && num2 != 0 && num3 == 0) {
      if (e.detail.value == 0) {
        this.setData({
          bookQuality: 0
        })
      }
    }

    if (num1 == 0 && num2 != 0 && num3 != 0) {
      if (e.detail.value == 0) {
        this.setData({
          bookQuality: 2
        })
      }


      if (e.detail.value == 1) {
        this.setData({
          bookQuality: 0
        })
      }
    }

    if (num1 == 0 && num2 == 0 && num3 != 0) {
      if (e.detail.value == 0) {
        this.setData({
          bookQuality: 2
        })
      }
    }



   _this.getDisconprice(_this.data.bookId, _this.data.bookQuality, 2)






  },

  getDisconprice(id, bookquality, dex) {
    var _this = this

    wx.showLoading({
      title: "计算中",
      icon: "none"
    })


    var url = wx.getStorageSync("token") != '' ? 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/book/bookinfo/findPrice/auth' : 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/book/bookinfo/findPrices'
    var params = {
      id: id,
      bookQuality: bookquality
    }
    wx.request({
      url:url,
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          wx.hideLoading()
          _this.setData({
            bookPrice: res.data.data.bookPrice,
            surplusPrice: res.data.data.surplusPrice,
            newBookAvailableNum: res.data.data.newBookAvailableNum,
            availableNum: res.data.data.availableNum,
            defAvailableNum: res.data.data.defAvailableNum
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          })
        }
      }
    })
  },


  //添加心愿单

  addXinyuan() {

    if (wx.getStorageSync('token') == '') {
      this.setData({
        istokenPop: true
      })
      return
    }
    var _this = this
    var params = {
      bookInfoId: this.data.bookId
    }
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/sdxWishList/mod/auth',
      data: params,
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 200) {
          _this.getStatus(_this.data.bookId)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },

  // 删除心愿单

  deleteXinyuan() {
    if (wx.getStorageSync('token') == '') {
      this.setData({
        istokenPop: true
      })
      return
    }
    var _this = this
    var params = {
      ids: this.data.xinyuanId
    }
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/sdxWishList/del/auth',
      data: params,
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 200) {
          _this.getStatus(_this.data.bookId)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },

  //查看是否添加心愿单

  getStatus(id) {
    var _this = this
    var params = {
      page: 1,
      size: 999999,
      userIds: wx.getStorageSync('userId'),
      bookInfoId: id
    }
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/sdxWishList/find/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: function (res) {
        console.log(res)

        if (res.data.code == 200) {

          var isCollect = res.data.data.length == 0 ? false : true

          _this.setData({
            isCollect: isCollect,
            xinyuanId: res.data.data.length != 0 ? res.data.data[0].id : ''
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },


  zhuanshubi () {
    wx.switchTab({
      url: '/pages/juanshu/juanshu',
    })
  },


  //获取相关推荐

  getSamebook (id) {


var _this = this
    var params = {
      id: id
    }
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/book/findSameKeyword/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: function (res) {
        if (res.data.code == 200) {
          _this.setData({
            sameBooks: res.data.data
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },


  //场景值解析接口

  getKey(params) {
    var _this = this
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/scene/values/auth',
      method: 'POST',
      data: params,
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.success) {
        _this.setData({
        bookId: res.data.data.userId,
        params: {
          page: 1,
          size: 5,
          bookInfoId: res.data.data.userId
        }
      })
      _this.getBookDetail(_this.data.bookId)
          if (wx.getStorageSync('token') != "") {
            _this.getCardnum(_this.data.bookId)
          }
      _this.getStatus(_this.data.bookId)
      _this.getYudingStatus(_this.data.bookId)
      _this.getFeelingsList(_this.data.params)

      _this.getSamebook(_this.data.bookId)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },


  //获取书币

  getsdxScores () {

    var _this= this
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/user/infos/auth',
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          _this.setData({
            sdxScores: res.data.data.sdxScores
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },

  onLoad(options) {

    this.setData({
      options: options
    })
    console.log('options.fromType====' + options.fromType)
    if (options.fromType) {
      this.setData({
        fromType: options.fromType
      })
    }


    if(options.scene) {
      // debugger
      var params = {
        sceneKey: decodeURIComponent(options.scene)
      }
      this.getKey(params)
      // this.setData({
      //   bookId: options.bookId,
      //   params: {
      //     page: 1,
      //     size: 5,
      //     bookInfoId: options.bookId
      //   }
      // })
      // this.getStatus(this.data.bookId)
      // this.getYudingStatus(this.data.bookId)
      // this.getFeelingsList(this.data.params)

      // this.getSamebook(this.data.bookId)
    } else {
      this.setData({
        bookId: this.data.options.bookId,
        params: {
          page: 1,
          size: 5,
          bookInfoId: options.bookId
        }
      })


      this.getBookDetail(this.data.bookId)
      if (wx.getStorageSync('token') != "") {
        this.getCardnum(this.data.bookId)
        this.getsdxScores()
      }
      this.getStatus(this.data.bookId)
      this.getYudingStatus(this.data.bookId)
      this.getFeelingsList(this.data.params)
      // this.getCardnum(this.data.bookId)

      this.getSamebook(this.data.bookId)
    }


  },

  onShow() {
    // this.getBookDetail(this.data.bookId)
  },

  // 打开卖书记录
  buybookList() {
    if (wx.getStorageSync('token') == '') {
      this.setData({
        istokenPop: true
      })
      return
    }
    wx.navigateTo({
      url: '/package/index/pages/buybookList/buybookList?bookId=' + this.data.bookId,
    })
  },

  //打开观后感
  lookFeeling(e) {
    console.log(e)


    if (wx.getStorageSync('token') == '') {
      this.setData({
        istokenPop: true
      })
      return
    }
    var feelid = e.currentTarget.dataset.feelid


    wx.navigateTo({
      url: '/package/index/pages/lookFeeling/lookFeeling?feelid=' + feelid + '&bookId=' + this.data.bookId + '&coverPic=' + this.data.data.coverPic + '&name=' + this.data.data.name,
    })


    return

    if (e.currentTarget.dataset.needbuy) {
      wx.navigateTo({
        url: '/package/index/pages/lookFeeling/lookFeeling?feelid=' + feelid + '&bookId=' + this.data.bookId + '&coverPic=' + this.data.data.coverPic + '&name=' + this.data.data.name,
      })
    } else {
      this.buyFeel(feelid)
    }
    // wx.navigateTo({
    //   url: '/package/index/pages/lookFeeling/lookFeeling?feelid=' + feelid,
    // })
  },


  //购买读书感

  buyFeel(id) {

    var _this = this
    var params = {
      id: id
    }

    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/book/afterReading/buy/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          wx.showToast({
            title: '购买成功',
            icon: 'none',
            duration: 2000
          })

          for (var i = 0; i < _this.data.list.length; i++) {
            if (id == _this.data.list[i].id) {
              var needBuy = "list[" + i + "].noNeedBuy"
              _this.setData({
                [needBuy]: true
              })
            }
          }
          // _this.getFeelingsList(_this.data.params)
          setTimeout(function () {
            wx.navigateTo({
              url: '/package/index/pages/lookFeeling/lookFeeling?feelid=' + id + '&bookId=' + _this.data.bookId + '&coverPic=' + _this.data.data.coverPic + '&name=' + _this.data.data.name,
            })
          }, 2000)
        } else {
          wx.showToast({
            title: res.data.data,
            icon: 'none'
          })
        }
      }
    })
  },

  goBuy8888 () {
    wx.showToast({
      title: '此书无库存',
      icon: 'none'
    })
  },


getWasphone () {

    var _this = this

    return new Promise(function (resolve, reject) {
    wx.request({
        url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/user/wasBindPhone/auth',
        method: 'POST',
        header: {
          "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
          "token": wx.getStorageSync('token'),
          "uid": wx.getStorageSync('uuid')
        },
        success: (res) => {
          if (res.data.code == 200) {
            resolve(res.data.data)
           
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        }
      })
    })
    
  },

  //去购买
  goBuy() {


var _this = this


    this.setData({
      chooseOpen: 2
    })


    if (wx.getStorageSync('token') == '') {
      this.setData({
        istokenPop: true
      })
      return
    }

    this.getWasphone().then(res=>{
      if (res!=1) {
        _this.setData({
          isnumPop: true
        })

        return
      }else{


        _this.setData({
          openPop2: true
        })

        _this.getsdxScores()
        // _this.setData({
        //   isnumPop: false
        // })
      }
    })



    // this.setData({
    //   openPop2: true
    // })


    return

    var arr = []

    var params = {
      bookId: this.data.bookId,
      coverPic: this.data.data.coverPic,
      name: this.data.data.name,
      price: this.data.data.price,

    }

    arr.push(params)
    wx.navigateTo({
      url: '/package/index/pages/buyBook/buyBook?bookArr=' + encodeURIComponent(JSON.stringify(arr))
    })
  },


  getFangshi9 (e) {

    if (wx.getStorageSync('token') == '') {
      this.setData({
        istokenPop: true
      })
      return
    }


    this.setData({
      choosefangshi: e.currentTarget.dataset.fangshi
    })

    var arr = []

    var params = {
      bookId: this.data.bookId,
      coverPic: this.data.coverPic,
      name: this.data.name,
      price: this.data.price
    }

    arr.push(params)



    var str = this.data.bookQuality == 1 ? -1 : this.data.bookQuality == 2 ? -3 : this.data.bookQuality == 0 ? -2 : this.data.bookQuality


    wx.navigateTo({
      url: '/package/index/pages/buyBook/buyBook?bookArr=' + encodeURIComponent(JSON.stringify(arr)) + '&bookIdstr=' + arr[0].bookId + '_' + str
    })
  },

  getFangshi3(e) {
    if (wx.getStorageSync('token') == '') {
      this.setData({
        istokenPop: true
      })
      return
    }


    this.setData({
      choosefangshi: e.currentTarget.dataset.fangshi
    })

    var arr = []

    var params = {
      bookId: this.data.bookId,
      coverPic: this.data.coverPic,
      name: this.data.name,
      price: this.data.price
    }

    arr.push(params)



    var str = this.data.bookQuality


    wx.navigateTo({
      url: '/package/index/pages/buyBook/buyBook?bookArr=' + encodeURIComponent(JSON.stringify(arr)) + '&bookIdstr=' + arr[0].bookId + '_' + str
    })

  },


  //获取书卷列表
  getbookQuan() {


    if (wx.getStorageSync('token') == '') {
      this.setData({
        istokenPop: true
      })
      return
    }
    var _this = this
    var params = {
      page: 1,
      size: 99999999,
      userId: wx.getStorageSync('userId'),
      isUsed: 0,
      expire: new Date().getTime()
    }

    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/sdxBookTickt/find/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          wx.showModal({
            title: '本次预定将消耗一张书券',
            content: '您共有' + res.data.count + '张',
            success: function (res) {
              if (res.confirm) {
                _this.yudingBook()
              } else {
                return false
              }
            }
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  //书卷预定
  yudingBook() {

    var _this = this
    var params = {
      id: this.data.bookId
    }
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/book/preOrder/add/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        // console.log(res)

        if (res.data.code == 200) {
          wx.showToast({
            title: '预定成功',
            icon: 'none'
          })
          _this.getYudingStatus(_this.data.bookId)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },

  //获取书卷预定状态
  getYudingStatus(bookId) {
    var _this = this
    var params = {
      bookInfoId: bookId
    }
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/book/preOrder/status/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          _this.setData({
            isYuding: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },


  //获取心得列表

  getFeelingsList(params) {
    var _this = this
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/book/afterReading/find/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          var arr = _this.data.list
          res.data.data.forEach(function (item, index) {
            arr.push(item)
          })
          _this.setData({
            count: res.data.count,
            list: arr
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },


  //点赞之后刷新页面

  dianzanLoad(id, isThumb, type, num) {
    console.log(type)
    console.log(num)
    for (var i = 0; i < this.data.list.length; i++) {
      if (id == this.data.list[i].id) {
        var typeNum = "list[" + i + "].thumbType"
        var thumbUpNum = "list[" + i + "].thumbUpNum"
        var thumb = "list[" + i + "].isThumb"
        this.setData({
          [typeNum]: type,
          [thumbUpNum]: num,
          [thumb]: isThumb
        })
      }
    }
  },


  //读书心得查看更多
  loadMore() {
    this.data.params.page += 1
    this.getFeelingsList(this.data.params)
  },

  //已经预定点击按钮
  yiYuding() {
    wx.showToast({
      title: '已预定，不要重复预定',
      icon: 'none'
    })
  },

  //查看更多信息
  lookMore() {

    this.setData({
      lookMore: false
    })
  },
  lookMore2() {

    this.setData({
      lookMore: true
    })
  },

  //跳转首页
  goHome() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },


  //添加购物车
  addCard() {
    if (wx.getStorageSync('token') == '') {
      this.setData({
        istokenPop: true
      })
      return
    }


    if (this.data.fromType == 1) {
      wx.navigateBack()
      return
    }
    wx.navigateTo({
      url: '/package/my/pages/shopCar/shopCar?fromType=3'
    })
    return

    this.setData({
      openPop2: true
    })

    return
    if (this.data.availableNum == 0) {
      wx.showToast({
        title: '库存不足',
        icon: 'none'
      })
      return
    }
    this.addCarApi(this.data.bookId)
  },


  touchMove() {
    this.setData({
      istokenPop: false,
      openPop2: false
    })
  },

  loginCode() {

    var _this = this
    this.setData({
      istokenPop: false
    })
    this.onLoad(this.data.options)

    this.getCardnum(this.data.bookId)
    


    this.getWasphone().then(res => {
      if (res!=1) {
        _this.setData({
          isnumPop: true
        })

        return
      }else{
        if (this.data.chooseOpen == 1) {
          _this.setData({
          isnumPop: false,
          openPop: true
        })
      }
        if (this.data.chooseOpen == 2) {
          _this.setData({
            isnumPop: false,
            openPop2: true
          })
      }
        
      }
    })


  },


  loginNum () {
    this.setData({
      isnumPop: false
    })
  },

  loginNum3 () {
    this.setData({
      isnumPop: false
    })
    if (this.data.chooseOpen == 1) {
      this.setData({
        openPop: true
      })
    }

    if (this.data.chooseOpen == 2) {
      this.setData({
        openPop2: true
      })
    }
  },

  // 获取购物车数量


  getCardnum(bookid) {
    var _this = this
    var url = wx.getStorageSync('token') != "" ? "https://devapi.xiaoshitimebank.com/user/sdx/sdx/book/findMyTrolleyBook/auth" :"https://devapi.xiaoshitimebank.com/user/sdx/sdx/book/findMyTrolleyBook"
    var params = {
      bookInfoId: bookid
    }
    wx.request({
      url: url,
      data: params,
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          _this.setData({
            TrolleyBookNum: res.data.data.TrolleyBookNum,
            wasTrolleyBook: res.data.data.wasTrolleyBook
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },

  changeData3 () {
    this.onLoad(this.data.options)
    this.onShow()
  },

  changeData8 () {
    this.getBookDetail(this.data.bookId)
    this.getCardnum(this.data.bookId)
  },




  onShareAppMessage() {
    var that = this;

    return {

      title: that.data.data.name + '￥' + that.data.surplusPrice,

      path: '/package/index/pages/bookdetail/bookdetail?bookId=' + that.data.bookId,

      imageUrl: that.data.data.coverPic,
      success: function (res) {

        console.log("转发成功:" + JSON.stringify(res));

        wx.showToast({
          title: '转发成功',
          icon: 'none'
        })

        // that.shareClick();

      }
    }
  },



  // 新添加立即购买


  likeMai2 () {
    wx.showToast({
      title: '无库存',
      icon:'none'
    })

    return
  },

  likeMai () {


    this.setData({
      chooseOpen: 1
    })

    var _this = this

    if (wx.getStorageSync('token') == '') {
      this.setData({
        istokenPop: true
      })
      return
    }

    this.getWasphone().then(res => {
      if (res != 1) {
        _this.setData({
          isnumPop: true
        })

        return
      } else {


        _this.setData({
          openPop: true
        })

        _this.getsdxScores()
        // _this.setData({
        //   isnumPop: false
        // })
      }
    })

  }



})
